package com.cd.dao;

import com.cd.entity.Movie;
import com.cd.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class MovieDAO {

	// 查询所有电影
	public List<Movie> findAll() throws SQLException {
		List<Movie> movies = new ArrayList<>();
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			conn = DBUtil.getConn();
			String sql = "SELECT * FROM movie";
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			while (rs.next()) {
				Long movieId = rs.getLong("movie_id");
				String title = rs.getString("title");
				String director = rs.getString("director");
				String releaseDate = rs.getString("release_date");
				Double rating = rs.getDouble("rating");
				Movie movie = new Movie(movieId, title, director, releaseDate, rating);
				movies.add(movie);
			}
		} finally {
			DBUtil.close(conn, rs, ps);
		}
		return movies;
	}

	// 添加电影
	public void add(Movie movie) throws SQLException {
		Connection conn = null;
		PreparedStatement ps = null;
		try {
			conn = DBUtil.getConn();
			String sql = "INSERT INTO movie (movie_id, title, director, release_date, rating) VALUES (?,?,?,?,?)";
			ps = conn.prepareStatement(sql);
			ps.setLong(1, movie.getMovieId());
			ps.setString(2, movie.getTitle());
			ps.setString(3, movie.getDirector());
			ps.setString(4, movie.getReleaseDate());
			ps.setDouble(5, movie.getRating());
			ps.executeUpdate();
		} finally {
			DBUtil.close(conn, null, ps);
		}
	}

	// 修改电影
	public void update(Movie movie) throws SQLException {
		Connection conn = null;
		PreparedStatement ps = null;
		try {
			conn = DBUtil.getConn();
			String sql = "UPDATE movie SET title = ?, director = ?, release_date = ?, rating = ? WHERE movie_id = ?";
			ps = conn.prepareStatement(sql);
			ps.setString(1, movie.getTitle());
			ps.setString(2, movie.getDirector());
			ps.setString(3, movie.getReleaseDate());
			ps.setDouble(4, movie.getRating());
			ps.setLong(5, movie.getMovieId());
			ps.executeUpdate();
		} finally {
			DBUtil.close(conn, null, ps);
		}
	}

	// 删除电影
	public void delete(Long movieId) throws SQLException {
		Connection conn = null;
		PreparedStatement ps = null;
		try {
			conn = DBUtil.getConn();
			String sql = "DELETE FROM movie WHERE movie_id = ?";
			ps = conn.prepareStatement(sql);
			ps.setLong(1, movieId);
			ps.executeUpdate();
		} finally {
			DBUtil.close(conn, null, ps);
		}
	}

	// 根据电影ID查询电影
	public Movie findById(Long movieId) throws SQLException {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		Movie movie = null;
		try {
			conn = DBUtil.getConn();
			String sql = "SELECT * FROM movie WHERE movie_id = ?";
			ps = conn.prepareStatement(sql);
			ps.setLong(1, movieId);
			rs = ps.executeQuery();
			if (rs.next()) {
				String title = rs.getString("title");
				String director = rs.getString("director");
				String releaseDate = rs.getString("release_date");
				Double rating = rs.getDouble("rating");
				movie = new Movie(movieId, title, director, releaseDate, rating);
			}
		} finally {
			DBUtil.close(conn, rs, ps);
		}
		return movie;
	}
}